package com.example.android_api;

import java.io.FileOutputStream;
import java.text.BreakIterator;

import com.example.android_api.util.Util;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SaveDataActivity extends Activity {

	private EditText editText;
	View focusView = null;
	boolean cancel = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_save_data);
		editText = (EditText) findViewById(R.id.savedataedit);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.save_date, menu);
		return true;
	}

	/**
	 * 保存为键值对
	 * 
	 * @Description
	 * @Author robinjiang
	 * @Date 2013-10-31 下午2:26:53
	 * 
	 * @Update_User robinjiang
	 * @Update_Date 2013-10-31 下午2:26:53
	 * @Update_Desprition
	 * 
	 * @param view
	 * @return void
	 * @throws
	 */
	public void saveAsKV(View view) {
		checkText();
		if (!cancel) {
			SharedPreferences sharedPref = this.getSharedPreferences(
					getString(R.string.app_name), Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = sharedPref.edit();
			editor.putString(getString(R.string.save_data_key_kv), editText
					.getText().toString());
			editor.commit();
			cancel = false;
			
			String highScore = sharedPref.getString(getString(R.string.save_data_key_kv), "no data");
			Toast.makeText(SaveDataActivity.this, "键值对保存成功！"+highScore, Toast.LENGTH_SHORT)
			.show();
		}
	}

	/**
	 * 保存为数据库
	 * 
	 * @Description
	 * @Author robinjiang
	 * @Date 2013-10-31 下午2:27:24
	 * 
	 * @Update_User robinjiang
	 * @Update_Date 2013-10-31 下午2:27:24
	 * @Update_Desprition
	 * 
	 * @param view
	 * @return void
	 * @throws
	 */
	public void saveAsSql(View view) {
		if (!cancel) {
			Toast.makeText(SaveDataActivity.this, "====", Toast.LENGTH_SHORT)
					.show();
		}
	}

	/**
	 * 保存为文件
	 * 
	 * @Description
	 * @Author robinjiang
	 * @Date 2013-10-31 下午2:27:42
	 * 
	 * @Update_User robinjiang
	 * @Update_Date 2013-10-31 下午2:27:42
	 * @Update_Desprition
	 * 
	 * @param view
	 * @return void
	 * @throws
	 */
	public void saveAsFile(View view) {
		cancel = Util.isExternalStorageWritable();
		if (cancel) {
			Util.getAlbumStorageDir(getString(R.string.app_name), Environment.DIRECTORY_PICTURES);
			Log.i("api", "创建文件成功");
		}else{
			String string = editText.getText().toString();
			FileOutputStream outputStream;

			try {
			  outputStream = openFileOutput(getString(R.string.app_name), Context.MODE_PRIVATE);
			  outputStream.write(string.getBytes());
			  outputStream.close();
			} catch (Exception e) {
			  e.printStackTrace();
			}
		}
	}
	
	//验证空间是否为空
	public void checkText() {
		if (TextUtils.isEmpty(editText.getText().toString())) {
			editText.setError(getString(R.string.error_field_required));
			focusView = editText;
			cancel = true;
		}
	}
}
